package com.keyinong.caizhengting.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.Utility;
import com.keyinong.caizhengting.adapter.CaidanAdapter;
import com.keyinong.caizhengting.entity.CaidanEntity;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 我的订单中团队订单详情
 */

public class MyorderteamorderActivity extends Activity {
    View iscatview = null;
    /**
     * 获得数据
     */
    ArrayList<CaidanEntity> infolist;
    HttpTools httptools = null;
    private CaidanAdapter caidanAdapter;
    /**
     * 菜单列表
     */
    private ListView lv;
    /**
     * 合计
     */
    private TextView tv_heji;
    /**
     * 返回
     */
    private ImageButton ib_fanhui;
    /**
     * 订单号
     */
    private TextView tv_dingdanhao;
    /**
     * 下单时间
     */
    private TextView tv_xiadan;
    /**
     * 取餐时间
     */
    private TextView tv_qucan;
    /**
     * 取餐方式
     */
    private TextView tv_recomode;
    /**
     * 支付方式
     */
    private TextView tv_paymodeid;
    /**
     * 备注
     */
    private TextView tv_beizhu;
    /**
     * 送货上门
     */
    private TextView tv_fee;
    private String total;
    /**
     * 退单
     */
    private Button btn_tuidan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_myorderteamorder);
        lv = (ListView) findViewById(R.id.lv_caidan2);
        tv_heji = (TextView) findViewById(R.id.textView9);
        tv_xiadan = (TextView) findViewById(R.id.tv_xiadan3);
        tv_qucan = (TextView) findViewById(R.id.tv_qucan);
        tv_dingdanhao = (TextView) findViewById(R.id.tv_dingdanhao2);
        ib_fanhui = (ImageButton) findViewById(R.id.ib_back);
        tv_recomode = (TextView) findViewById(R.id.tv_recomde);
        tv_paymodeid = (TextView) findViewById(R.id.tv_paymode);
        tv_beizhu = (TextView) findViewById(R.id.tv_beizhu);
        tv_fee = (TextView) findViewById(R.id.tv_fee);
        ib_fanhui.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                //Intent intent = new Intent();
                //intent.setClass(getApplicationContext(), FanqiedingcanActivity.class);
                //startActivity(intent);
            }
        });
        btn_tuidan = (Button) findViewById(R.id.btn_shengqing);
        btn_tuidan.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                cancledialog();
            }
        });
        Caidan();
    }

    private void Caidan() {
        infolist = new ArrayList<CaidanEntity>();
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        String orderid = getIntent().getStringExtra("orderid");
        Log.i("info", "orderid---" + orderid);
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.orderdetail(token, orderid, new JsonCaidan());

    }

    /**
     * 取消订单
     */
    private void cancledialog() {

        final Dialog dialog = new Dialog(MyorderteamorderActivity.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        iscatview = LayoutInflater.from(MyorderteamorderActivity.this).inflate(
                R.layout.dialog_phone, null);
        //			TextView tital = (TextView) iscatview.findViewById(R.id.tital);
        //			TextView tv_phone = (TextView) iscatview.findViewById(R.id.tv_phone);
        TextView tital = (TextView) iscatview.findViewById(R.id.tital);
        TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no2);
        TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes2);
        tital.setText("是否取消该订单？");
        tv_no.setText("取消");
        tv_yes.setText("确定");
//		note=et_reason.getText().toString();
        //			tv_phone.setText(phone);
        //			tv_phone.setVisibility(View.GONE);
        //			tital.setText("该上下班有人加入，拼车是否完成?");
        dialog.setContentView(iscatview);
        dialog.show();
        tv_no.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        tv_yes.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
//				cancle();
                dialog.dismiss();
            }
        });
    }

    /**
     * 取消订单
     */
//	public void cancle(){
//		SharedPreferences sp = getSharedPreferences("spReg",
//				Context.MODE_PRIVATE);
//		String token = sp.getString("data", "");
//		String orderid = getIntent().getStringExtra("orderid");
//		if (httptools == null) {
//			httptools = new HttpTools(getApplicationContext());
//		}
//		httptools.cancelorder(token, orderid, new JsonHttpResponseHandler(){
//			@Override
//			public void onSuccess(int statusCode, JSONObject response) {
//				// TODO Auto-generated method stub
//				super.onSuccess(statusCode, response);
//				try {
//					boolean code = response.getBoolean("code");
//					String msg = response.getString("msg");
//					if(code==true){
//						Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
//						btn_tuidan.setVisibility(View.GONE);
//					}else{
//						Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
//					}
//				} catch (JSONException e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

    public class JsonCaidan extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                //Log.i("zq",response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    Log.i("shi", "订单详情---" + response.toString());
                    JSONObject object = response.getJSONObject("data");
                    String id = object.getString("id");
                    String order_time = object.getString("order_time");
                    String receive_time = object.getString("receive_time");
                    total = object.getString("total");
                    String state = object.getString("state");
                    String orderno = object.getString("orderno");
                    String paymodeid = object.getString("paymodeid");
                    String recmode = object.getString("recmode");
                    String note = object.getString("note");
                    String fee = object.getString("fee");
                    JSONArray array = object.getJSONArray("details");
                    for (int j = 0; j < array.length(); j++) {
                        JSONObject details = array.getJSONObject(j);
                        String name = details.getString("name");
                        String amount = details.getString("amount");
                        String price = details.getString("price");
                        String sname = details.getString("sname");
                        infolist.add(new CaidanEntity(amount, name, price, sname));
                    }
//					SharedPreferences spuser = getSharedPreferences("id",
//							Context.MODE_PRIVATE);
//					Editor editorid = spuser.edit();
//					editorid.putString("id", id);
//					editorid.commit();
                    if (state.equals("0")) {
                        btn_tuidan.setText("取消订单");
                        btn_tuidan.setVisibility(View.VISIBLE);
                    } else {
                        btn_tuidan.setVisibility(View.GONE);
                    }
                    if (paymodeid.equals("1")) {
                        tv_paymodeid.setText("支付宝支付");
                    } else if (paymodeid.equals("3")) {
                        tv_paymodeid.setText("余额支付");
                    } else if (paymodeid.equals("2")) {
                        tv_paymodeid.setText("微信支付");
                    } else {
                        tv_paymodeid.setText("未支付");
                    }

                    if (recmode.equals("2")) {
                        tv_recomode.setText("送货上门");
                        tv_fee.setVisibility(View.VISIBLE);
                        tv_fee.setText("(含运费￥" + fee + ")");
                    } else {
                        tv_recomode.setText("店内自提");
                        tv_fee.setVisibility(View.INVISIBLE);
                    }
                    if (note.equals("") || note.equals("null")) {
                        tv_beizhu.setText("无");
                    } else {
                        tv_beizhu.setText(note);
                    }
                    if (!fee.equals("0")) {
                        double yunfei = Double.parseDouble(total) + Double.parseDouble(fee);
                        total = "" + yunfei;
                    }
                    tv_heji.setText(total);
                    tv_xiadan.setText(order_time);
                    tv_qucan.setText(receive_time.substring(5, 16));
//					tv_receive.setText(receive_time.substring(10, 16));
                    tv_dingdanhao.setText(orderno);
                    caidanAdapter = new CaidanAdapter(getApplicationContext(), infolist);
                    lv.setAdapter(caidanAdapter);
                    lv.setDivider(null);
                    Utility.setListViewHeightBasedOnChildren(lv);
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
